layui.define(['jquery', 'request'], function(exports) {
	"use strict";
	/**
	 * @since Pear Admin 4.0
	 * 
	 * Button component
	 * */
	var MOD_NAME = 'permission',
		$ = layui.jquery,
		request = layui.request;

	var permission = new function() {

		this.validPerm = function() {
			var userPermission = JSON.parse(sessionStorage.getItem("perm"));
			if (userPermission && userPermission.length > 0) {
				showPermission(userPermission);
			} else {
				request.post("permission/listUserPermission").then(res => {
					
					sessionStorage.setItem("perm",JSON.stringify(res.data))
					showPermission(res.data);
				})
			}

		}
	}

	function showPermission(data) {
		$("[data-perm]").each(function() {
			var $button = $(this);
			var permission = $(this).data("perm") || '';
			if (permission.indexOf("?") > 0) {
				permission = permission.split("?")[0];
			}
			if (data.some(function(item) {
				return item.url == permission;
			})) {
				$button.removeClass("layui-hide");
			} else {
				$button.remove();
			}
		})
	};


	exports(MOD_NAME, permission);
});
